package com.guigu.pd.work.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.common.utils.PageVo;
import com.guigu.pd.oms.service.OrderService;
import com.guigu.pd.serviceapi.authfeign.UserFeign;
import com.guigu.pd.serviceapi.omsfeign.OrderFeign;
import com.guigu.pd.serviceapi.workfeign.TransportOrderFeign;
import com.guigu.pd.work.entity.TransportOrderEntity;
import com.guigu.pd.work.service.TransportOrderService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.common.utils.PageUtils;
import com.guigu.common.utils.Query;

import com.guigu.pd.work.dao.TaskPickupDispatchDao;
import com.guigu.pd.serviceapi.pojo.TaskPickupDispatchEntity;
import com.guigu.pd.work.service.TaskPickupDispatchService;


@Service("taskPickupDispatchService")
public class TaskPickupDispatchServiceImpl extends ServiceImpl<TaskPickupDispatchDao, TaskPickupDispatchEntity> implements TaskPickupDispatchService {

    @Autowired
    private TransportOrderFeign transportOrderFeign;

    @Autowired
    private OrderFeign orderFeign;

    @Autowired
    private UserFeign usersFeign;

    @Autowired(required = false)
    private OrderService orderService;

    @Autowired
    private TransportOrderService transportOrderService;

    /**
     * 取件
     *
     * @return
     */
    @Override
    public PageVo<TaskPickupDispatchEntity> queryQJtaskPage(Map<String, Object> params) {
        TaskPickupDispatchEntity
                taskPickupDispatch = JSON.parseObject(JSON.toJSONString(params),
                TaskPickupDispatchEntity.class);
        Integer pageno = Integer.parseInt(params.get("pageno").toString());
        Integer pagesize = Integer.parseInt(params.get("pagesize").toString());

        System.out.println("taskPickupDispatch" + taskPickupDispatch);

        LambdaQueryWrapper<TaskPickupDispatchEntity> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(taskPickupDispatch.getCourierId()) && !taskPickupDispatch.getCourierId().equals("请选择")) {
            queryWrapper.like(StringUtils.isNotBlank(taskPickupDispatch.getCourierId()) && taskPickupDispatch.getCourierId() != "请选择",
                    TaskPickupDispatchEntity::getCourierId, taskPickupDispatch.getCourierId());
        }
        queryWrapper.like(taskPickupDispatch.getStatus() != null,
                TaskPickupDispatchEntity::getStatus, taskPickupDispatch.getStatus());

        if (StringUtils.isNotBlank(taskPickupDispatch.getTransportorderid())) {
            TransportOrderEntity transportOrder =
                    transportOrderService.getById(taskPickupDispatch.getTransportorderid());
            queryWrapper.like(StringUtils.isNotBlank(taskPickupDispatch.getTransportorderid()),
                    TaskPickupDispatchEntity::getOrderId, transportOrder.getOrderId());
        }

        if (StringUtils.isNotBlank(taskPickupDispatch.getSenderaddress())) {
            String orderid = orderFeign.getbySendaddress(taskPickupDispatch.getSenderaddress());
            queryWrapper.like(StringUtils.isNotBlank(taskPickupDispatch.getSenderaddress()),
                    TaskPickupDispatchEntity::getOrderId, orderid);
        }

        if (StringUtils.isNotBlank(taskPickupDispatch.getSendername())) {
            String orderid = orderFeign.getbySendname(taskPickupDispatch.getSendername());
            queryWrapper.like(StringUtils.isNotBlank(taskPickupDispatch.getSendername()),
                    TaskPickupDispatchEntity::getOrderId, orderid);
        }

        PageHelper.startPage(pageno, pagesize);
        List<TaskPickupDispatchEntity> taskPickup = this.list(queryWrapper);

        List<TaskPickupDispatchEntity> taskPickuplist = new ArrayList<TaskPickupDispatchEntity>();

        for (TaskPickupDispatchEntity pickup : taskPickup) {
            Integer taskType = pickup.getTaskType();
            //查取件任务
            if (taskType == 1 && pickup.getStatus() != 5) {
                String orderId = pickup.getOrderId();
                //运单id查询    获取运单编号
                String transportOrderId = transportOrderFeign.getTransportbyorderId(orderId);
                //订单查询
                String order3 = orderFeign.getsendorderbyId(orderId);
                String[] split = order3.split(",");
                Date time = orderFeign.getordertimebyId(orderId);
                //用户查询（查快递员）
                if (pickup.getCourierId() != null && pickup.getCourierId() != "请选择") {
                    String user = usersFeign.getUser(pickup.getCourierId());
                    pickup.setCourier(user);
                }
                pickup.setTransportorderid(transportOrderId);
                pickup.setOrdercreatetime(time);
                pickup.setSendername(split[0]);
                pickup.setSenderphone(split[1]);
                pickup.setSenderaddress(split[2]);
                taskPickuplist.add(pickup);
            }
        }

        PageInfo<TaskPickupDispatchEntity> pageInfo = new PageInfo<TaskPickupDispatchEntity>(taskPickuplist);

        PageVo<TaskPickupDispatchEntity> pageVo = new PageVo<TaskPickupDispatchEntity>();
        pageVo.setRows(pageInfo.getList());
        pageVo.setTotal(pageInfo.getTotal());

        return pageVo;
    }

    /**
     * 派件
     *
     * @return
     */
    @Override
    public PageVo<TaskPickupDispatchEntity> queryPJtaskPage(Map<String, Object> params) {

        TaskPickupDispatchEntity
                taskPickupDispatch = JSON.parseObject(JSON.toJSONString(params),
                TaskPickupDispatchEntity.class);
        Integer pageno = Integer.parseInt(params.get("pageno").toString());
        Integer pagesize = Integer.parseInt(params.get("pagesize").toString());

        System.out.println("taskPickupDispatch" + taskPickupDispatch);

        LambdaQueryWrapper<TaskPickupDispatchEntity> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(taskPickupDispatch.getCourierId()) && !taskPickupDispatch.getCourierId().equals("请选择")) {
            queryWrapper.like(StringUtils.isNotBlank(taskPickupDispatch.getCourierId()) && taskPickupDispatch.getCourierId() != "请选择",
                    TaskPickupDispatchEntity::getCourierId, taskPickupDispatch.getCourierId());
        }
        queryWrapper.like(taskPickupDispatch.getStatus() != null,
                TaskPickupDispatchEntity::getStatus, taskPickupDispatch.getStatus());

        if (StringUtils.isNotBlank(taskPickupDispatch.getTransportorderid())) {
            TransportOrderEntity transportOrder =
                    transportOrderService.getById(taskPickupDispatch.getTransportorderid());
            queryWrapper.like(StringUtils.isNotBlank(taskPickupDispatch.getTransportorderid()),
                    TaskPickupDispatchEntity::getOrderId, transportOrder.getOrderId());
        }

        if (StringUtils.isNotBlank(taskPickupDispatch.getReceiveraddress())) {
            String orderid = orderFeign.getbyReceiveraddress(taskPickupDispatch.getReceiveraddress());
            queryWrapper.like(StringUtils.isNotBlank(taskPickupDispatch.getReceiveraddress()),
                    TaskPickupDispatchEntity::getOrderId, orderid);
        }

        if (StringUtils.isNotBlank(taskPickupDispatch.getReceivername())) {
            String orderid = orderFeign.getbyReceivername(taskPickupDispatch.getReceivername());
            queryWrapper.like(StringUtils.isNotBlank(taskPickupDispatch.getReceivername()),
                    TaskPickupDispatchEntity::getOrderId, orderid);
        }

        PageHelper.startPage(pageno, pagesize);
        List<TaskPickupDispatchEntity> taskPickup = this.list(queryWrapper);

        List<TaskPickupDispatchEntity> taskPickuplist = new ArrayList<TaskPickupDispatchEntity>();

        for (TaskPickupDispatchEntity pickup : taskPickup) {
            Integer taskType = pickup.getTaskType();
            //查取件任务
            if (taskType == 2 && pickup.getStatus() != 5) {
                String orderId = pickup.getOrderId();
                //运单id查询    获取运单编号
                String transportOrderId = transportOrderFeign.getTransportbyorderId(orderId);
                //订单查询
                String order3 = orderFeign.getsendorderbyId(orderId);
                String[] split = order3.split(",");
                Date time = orderFeign.getordertimebyId(orderId);
                //用户查询（查快递员）
                if (pickup.getCourierId() != null && pickup.getCourierId() != "请选择") {
                    String user = usersFeign.getUser(pickup.getCourierId());
                    pickup.setCourier(user);
                }
                pickup.setTransportorderid(transportOrderId);
                pickup.setOrdercreatetime(time);
                pickup.setReceivername(split[0]);
                pickup.setReceiverphone(split[1]);
                pickup.setReceiveraddress(split[2]);
                taskPickuplist.add(pickup);
            }
        }

        PageInfo<TaskPickupDispatchEntity> pageInfo = new PageInfo<TaskPickupDispatchEntity>(taskPickuplist);

        PageVo<TaskPickupDispatchEntity> pageVo = new PageVo<TaskPickupDispatchEntity>();
        pageVo.setRows(pageInfo.getList());
        pageVo.setTotal(pageInfo.getTotal());

        return pageVo;

    }

    /**
     * 取消件
     *
     * @return
     */
    @Override
    public PageVo<TaskPickupDispatchEntity> queryCJtaskPage(Map<String, Object> params) {
        TaskPickupDispatchEntity taskPickupDispatch = JSON.parseObject(JSON.toJSONString(params),
                TaskPickupDispatchEntity.class);
        Integer pageno = Integer.parseInt(params.get("pageno").toString());
        Integer pagesize = Integer.parseInt(params.get("pagesize").toString());

        LambdaQueryWrapper<TaskPickupDispatchEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(taskPickupDispatch.getStatus() != null,
                TaskPickupDispatchEntity::getStatus, 5);

        PageHelper.startPage(pageno, pagesize);
        List<TaskPickupDispatchEntity> taskPickup = this.list(queryWrapper);

        List<TaskPickupDispatchEntity> taskPickuplist = new ArrayList<TaskPickupDispatchEntity>();

        for (TaskPickupDispatchEntity pickup : taskPickup) {
            Integer taskType = pickup.getTaskType();
            //查取件任务
            if (taskType == 1 && pickup.getStatus() == 5) {
                String orderId = pickup.getOrderId();
                //运单id查询    获取运单编号
                String transportOrderId = transportOrderFeign.getTransportbyorderId(orderId);
                //订单查询
                String order3 = orderFeign.getsendorderbyId(orderId);
                String[] split = order3.split(",");
                Date time = orderFeign.getordertimebyId(orderId);
                //用户查询（查快递员）
                String user = usersFeign.getUser(pickup.getCourierId());
                pickup.setTransportorderid(transportOrderId);
                pickup.setOrdercreatetime(time);
                pickup.setCourier(user);
                pickup.setSendername(split[0]);
                pickup.setSenderphone(split[1]);
                pickup.setSenderaddress(split[2]);

                taskPickuplist.add(pickup);
            }
        }

        PageInfo<TaskPickupDispatchEntity> pageInfo = new PageInfo<TaskPickupDispatchEntity>(taskPickuplist);

        PageVo<TaskPickupDispatchEntity> pageVo = new PageVo<TaskPickupDispatchEntity>();
        pageVo.setRows(pageInfo.getList());
        pageVo.setTotal(pageInfo.getTotal());

        return pageVo;
    }

}
